package com.tiantianfresh.admin.controller;

import com.tiantianfresh.admin.service.RotationchartService;
import com.tiantianfresh.model.pojo.AJAXResult;
import com.tiantianfresh.model.pojo.Rotationchart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/rotationchart")
public class RotationchartController {
    @Autowired
    private RotationchartService rotationchartService;

    //添加到添加轮播图信息页面
    @RequestMapping("/toadd")
    public String add() {
        return "add";
    }

    //添加轮播图信息
    @ResponseBody
    @RequestMapping("/add")
    public Object addRotationchart(Rotationchart rotationchart) {
        AJAXResult result = new AJAXResult();
        result.setSuccess(rotationchartService.addRotationchart(rotationchart));
        return result;
    }

    @RequestMapping("/goRotationchart")
    public String goRotationchart() {
        return "rotationchart/index";
    }

    //查询全部Rotationchart页面
    @ResponseBody
    @RequestMapping("/allRotationchart")
    public Object list() {
        List<Rotationchart> rotationcharts = rotationchartService.selectAll();
        return rotationcharts;
    }

    //根据id删除一个轮播图
    @ResponseBody
    @RequestMapping("/updateCommodityorder")
    public Object updateCommodityorder(Integer id, Integer commodityorder) {
        Map<String, Integer> map = new HashMap<>();
        map.put("id", id);
        map.put("commodityorder", commodityorder);
        AJAXResult result = new AJAXResult();
        if (rotationchartService.deleteRotationchartById(map) > 0) {
            result.setSuccess(true);
        } else {
            result.setSuccess(false);
        }
        return result;
    }

    //跳转到修改书籍页面
    @RequestMapping("/toupdate/{id}")
    public String update(@PathVariable int id, Model model) {
        Rotationchart rotationchart = rotationchartService.queryRotationchartById(id);
        model.addAttribute("Rotationchart", rotationchart);
        return "update";
    }

    //保存修改信息
    @ResponseBody
    @RequestMapping("/update")
    public Object update(Rotationchart rotationchart) {
        AJAXResult result = new AJAXResult();
        result.setSuccess(rotationchartService.updateRotationchart(rotationchart));
        return result;
    }

    @ResponseBody
    @RequestMapping("/deltePermission")
    public Object deltePermission(Integer id) {
        AJAXResult result = new AJAXResult();
        result.setSuccess(rotationchartService.deltePermission(id));
        return result;
    }
}